
package com.anjiplus.template.gaea.business.modules.datasettransform.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.anji.plus.gaea.curd.mapper.GaeaBaseMapper;
import com.anjiplus.template.gaea.business.modules.datasettransform.controller.dto.DataSetTransformDto;
import com.anjiplus.template.gaea.business.modules.datasettransform.dao.DataSetTransformMapper;
import com.anjiplus.template.gaea.business.modules.datasettransform.dao.entity.DataSetTransform;
import com.anjiplus.template.gaea.business.modules.datasettransform.service.DataSetTransformService;
import com.anjiplus.template.gaea.business.modules.datasettransform.service.TransformStrategy;
import com.anjiplus.template.gaea.business.modules.httpdatasettransform.controller.dto.HttpDataSetTransformDto;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Raod
 * @desc DataSetTransform 数据集数据转换服务实现
 * @date 2021-03-18 12:13:15.591309400
 **/
@Service
//@RequiredArgsConstructor
public class DataSetTransformServiceImpl implements DataSetTransformService, InitializingBean, ApplicationContextAware {

    private final Map<String, TransformStrategy> queryServiceImplMap = new HashMap<>();
    private ApplicationContext applicationContext;

    @Autowired
    private DataSetTransformMapper dataSetTransformMapper;

    @Override
    public GaeaBaseMapper<DataSetTransform> getMapper() {
        return dataSetTransformMapper;
    }

    public TransformStrategy getTarget(String type) {
        return queryServiceImplMap.get(type);
    }

    @Override
    public void afterPropertiesSet() {
        Map<String, TransformStrategy> beanMap = applicationContext.getBeansOfType(TransformStrategy.class);
        //遍历该接口的所有实现，将其放入map中
        queryServiceImplMap.putAll(beanMap.values().stream()
                .collect(Collectors.toMap(TransformStrategy::type, Function.identity())));
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public List<JSONObject> transform(List<DataSetTransformDto> dataSetTransformDtoList, List<JSONObject> data, String relationalDataSet) {
        if (dataSetTransformDtoList == null || dataSetTransformDtoList.size() <= 0) {
            return data;
        }

        for (DataSetTransformDto dataSetTransformDto : dataSetTransformDtoList) {
            data = getTarget(dataSetTransformDto.getTransformType()).transform(dataSetTransformDto, data, relationalDataSet);
        }
        return data;
    }


    /**
     * http结果最后的处理 仅添加日志等 无需返回参数
     * 结果属于后置
     *
     * @param dataSetTransformDtoList
     * @param data
     * @return java.util.List<com.alibaba.fastjson.JSONObject>
     * @author WanShen
     * @date 2025/4/3 下午4:16
     */
    public String httpResultTransform(List<HttpDataSetTransformDto> dataSetTransformDtoList, String data) {
        if (dataSetTransformDtoList == null || dataSetTransformDtoList.size() <= 0) {
            return data;
        }

        for (HttpDataSetTransformDto dataSetTransformDto : dataSetTransformDtoList) {
            data = getTarget(dataSetTransformDto.getTransformType()).httpTransform(dataSetTransformDto, data);
        }
        return data;
    }

    /**
     * Http请求结果的前置参数处理 处理后再发给接口
     *
     * @param dataSetTransformDtoList
     * @param data
     * @return java.util.List<com.alibaba.fastjson.JSONObject>
     * @author WanShen
     * @date 2025/4/14 下午2:37
     */
    public String beforeHttpResultTransform(List<HttpDataSetTransformDto> dataSetTransformDtoList, String data) {
        if (dataSetTransformDtoList == null || dataSetTransformDtoList.size() <= 0) {
            return data;
        }

        for (HttpDataSetTransformDto dataSetTransformDto : dataSetTransformDtoList) {
           data =  getTarget(dataSetTransformDto.getTransformType()).httpTransform(dataSetTransformDto, data);
        }
        return data;
    }
}
